package com.sanalkitaplik.admin;

import org.primefaces.model.DualListModel;

import com.sanalkitaplik.hibernateutil.AuthorizationsUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

public class AdminGroupsManager {

	private String newGroupName;
	private String addedGroupName;
	private String editingGroupName;
	private String editedGroupName;
	private String removingGroupName;
	private String removedGroupName;

	private boolean addingGroupSuccessful;
	private boolean editingGroupSuccessful;
	private boolean editGroupSelected;
	private boolean removingGroupSuccessful;
	
	private DualListModel<String> addingAuthorizations;
	private DualListModel<String> editingAuthorizations;
	private List<SelectItem> groupList;

	public AdminGroupsManager() {
		setNewGroupName("");
		setAddedGroupName("");
		setAddingGroupSuccessful(false);
		setEditingGroupSuccessful(false);
		setRemovingGroupSuccessful(false);
		setEditGroupSelected(false);
		
		loadGroupList();
		loadAddingAuthorizations();
	}

	public void setNewGroupName(String newGroupName) {
		this.newGroupName = newGroupName;
	}

	public String getNewGroupName() {
		return this.newGroupName;
	}

	public void setAddingAuthorizations(DualListModel<String> addingAuthorizations) {
		this.addingAuthorizations = addingAuthorizations;
	}

	public DualListModel<String> getAddingAuthorizations() {
		return this.addingAuthorizations;
	}
	
	public void setEditGroupSelected(boolean editGroupSelected) {
		this.editGroupSelected = editGroupSelected;
	}
	
	public boolean getEditGroupSelected() {
		return this.editGroupSelected;
	}
	
	public void selectEditGroup(ActionEvent event) {
		loadEditingAuthorizations();
		setEditGroupSelected(true);
		setEditingGroupSuccessful(false);
	}
	
	public void addNewGroup(ActionEvent event) {
		try {
			if (AuthorizationsUtil.addAdminGroup(newGroupName)) {
				List<String> authorizations = addingAuthorizations.getTarget();
				for (int i=0; i<authorizations.size(); i++) {
					if (!AuthorizationsUtil.addAuthorizationToGroup(newGroupName, authorizations.get(i)))
						return;
				}
				addedGroupName = newGroupName;
				newGroupName = "";
				loadAddingAuthorizations();
				addingGroupSuccessful = true;
				loadGroupList();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void setAddedGroupName(String addedGroupName) {
		this.addedGroupName = addedGroupName;
	}

	public String getAddedGroupName() {
		return addedGroupName;
	}

	public void setAddingGroupSuccessful(boolean addingGroupSuccessful) {
		this.addingGroupSuccessful = addingGroupSuccessful;
	}

	public boolean getAddingGroupSuccessful() {
		return addingGroupSuccessful;
	}

	public void setEditingAuthorizations(DualListModel<String> editingAuthorizations) {
		this.editingAuthorizations = editingAuthorizations;
	}

	public DualListModel<String> getEditingAuthorizations() {
		return editingAuthorizations;
	}
	
	public void setGroupList(List<SelectItem> groupList) {
		this.groupList = groupList;
	}

	public List<SelectItem> getGroupList() {
		return groupList;
	}
	
	public String getEditingGroupName() {
		return editingGroupName;
	}

	public void setEditingGroupName(String editingGroupName) {
		this.editingGroupName = editingGroupName;
	}
	
	public void setEditingGroupSuccessful(boolean editingGroupSuccessful) {
		this.editingGroupSuccessful = editingGroupSuccessful;
	}

	public boolean getEditingGroupSuccessful() {
		return editingGroupSuccessful;
	}
	
	public String getEditedGroupName() {
		return editedGroupName;
	}
	
	public void setRemovingGroupName(String removingGroupName) {
		this.removingGroupName = removingGroupName;
	}

	public String getRemovingGroupName() {
		return removingGroupName;
	}
	
	public void saveGroupSettings(ActionEvent event) {
		try {
			if (AuthorizationsUtil.deleteAuthorizationsByGroup(editingGroupName)) {
				List<String> target = editingAuthorizations.getTarget();
				for (int i=0; i<target.size(); i++) {
					if (!AuthorizationsUtil.addAuthorizationToGroup(editingGroupName, target.get(i)))
						return;
				}
			}
			setEditGroupSelected(true);
			setEditGroupSelected(false);
			editedGroupName = editingGroupName;
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void removeGroup(ActionEvent event) {
		try {
			if (AuthorizationsUtil.removeAdminGroup(removingGroupName)) {
				setRemovingGroupSuccessful(true);
				setRemovedGroupName(removingGroupName);
				loadGroupList();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void loadGroupList() {
		if (groupList == null)
			groupList = new ArrayList<SelectItem>();
		else
			groupList.clear();
		List<String> list = AuthorizationsUtil.getGroupNames();
		for (int i=0; i<list.size(); i++)
			groupList.add(new SelectItem(list.get(i)));
		
	}
	
	private void loadAddingAuthorizations() {
		List<String> authorizationsSource = AuthorizationsUtil.getAuthorizations();
		List<String> authorizationsTarget = new ArrayList<String>();  

		addingAuthorizations = new DualListModel<String>(authorizationsSource, authorizationsTarget); 
	}
	
	private void loadEditingAuthorizations() {
		List<String> target = AuthorizationsUtil.getGroupAuthorizations(editingGroupName);
		if (target != null) {
			List<String> source = AuthorizationsUtil.getAuthorizations();
			for (int i=0; i<target.size(); i++) {
				for (int j=0; j<source.size(); j++) {
					if (target.get(i).equals(source.get(j))) {
						source.remove(j);
						break;
					}
				}
			}
			editingAuthorizations = new DualListModel<String>(source, target);
		}
	}

	public void setRemovingGroupSuccessful(boolean removingGroupSuccessful) {
		this.removingGroupSuccessful = removingGroupSuccessful;
	}

	public boolean getRemovingGroupSuccessful() {
		return removingGroupSuccessful;
	}

	public void setRemovedGroupName(String removedGroupName) {
		this.removedGroupName = removedGroupName;
	}

	public String getRemovedGroupName() {
		return removedGroupName;
	}
	
}
